package com.jtjrenren.android.taxi.passenger.core.pay.main;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.anbang.pay.sdk.BangfubaoHelper;
import com.jtjrenren.android.taxi.passenger.base.BaseApplication;
import com.jtjrenren.android.taxi.passenger.core.wallect.coupons.CouponsActivity;
import com.jtjrenren.android.taxi.passenger.bean.api.ApiCouList;
import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.base.BaseMvpActivity;
import com.jtjrenren.android.taxi.passenger.core.pay.result.PayResultActivity;
import com.jtjrenren.android.taxi.passenger.utils.UIHelper;
import com.library.utils.StringUtils;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * Created by Administrator on 2016/1/29.
 */
public class PayActivity extends BaseMvpActivity<PayPresenter> implements PayView {
    @BindView(R.id.pay_main_qianbaozhifu)
    LinearLayout ll_qb;
    @BindView(R.id.pay_main_xianjinzhifu)
    LinearLayout ll_crash;
    @BindView(R.id.pay_main_qianbaozhifu_cb)
    CheckBox cb_qb;
    @BindView(R.id.pay_main_alizhifu)
    LinearLayout ll_ali;
    @BindView(R.id.pay_main_alizhifu_cb)
    CheckBox cb_ali;
    @BindView(R.id.pay_main_weixinzhifu)
    LinearLayout ll_wx;
    @BindView(R.id.pay_main_weixinzhifu_cb)
    CheckBox cb_weixin;
    @BindView(R.id.pay_main_xianjinzhifu_cb)
    CheckBox cb_crash;
    @BindView(R.id.pay_main_bfbzf)
    LinearLayout ll_bfb;
    @BindView(R.id.pay_main_bfbzf_cb)
    CheckBox cb_bfb;

    @BindView(R.id.pay_orderpay_coupons_count)
    TextView tv_orderpay_couponscount;
    @BindView(R.id.pay_orderpay_dealcount)
    TextView tv_orderpay_dealcount;
    @BindView(R.id.pay_main_qianbaozhifu_ye)
    TextView tv_qbye;
    @BindView(R.id.pay_orderpay_count)
    EditText tv_orderpay_count;

    @BindView(R.id.pay_orderpay_tip_con)
    LinearLayout ll_tip;
    @BindView(R.id.pay_orderpay_tip)
    TextView tv_tip;
    @BindView(R.id.title)
    TextView tv_title;

    public static Intent getDiyIntent(Activity context, String orderID) {
        Intent intent = new Intent(context, PayActivity.class);
        intent.putExtra("orderID", orderID);
        return intent;
    }

    @Override
    public int getLayoutId() {
        return R.layout.aty_pay_main;
    }

    @Override
    public PayPresenter createPresenter() {
        return new PayPresenterImpl(this);
    }

    @Override
    public void initM() {
        tv_title.setText("支付");
        presenter.init();
        initHandler();

        presenter.initData(getIntent().getStringExtra("orderID"));

        cb_ali.setChecked(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.destory();
    }

    @Override
    public void crashPaySucess() {
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void showPayTaxiView() {
        ll_tip.setVisibility(View.VISIBLE);
        ll_qb.setVisibility(View.VISIBLE);
        ll_wx.setVisibility(View.VISIBLE);
        ll_ali.setVisibility(View.VISIBLE);
        ll_crash.setVisibility(View.VISIBLE);
        ll_bfb.setVisibility(View.VISIBLE);
    }

    @Override
    public void showPaySpecarView() {
        ll_tip.setVisibility(View.GONE);
        tv_orderpay_count.setEnabled(false);
        ll_crash.setVisibility(View.GONE);
    }

    @Override
    public void refreshOrderMoney(String money) {
        tv_orderpay_count.setText(money);
    }

    @Override
    public void refreshUserMoney(String money) {
        tv_qbye.setText("(" + money + ")");
    }

    @Override
    public void refreshTipMoney(String money) {
        tv_tip.setText(money);
    }

    @Override
    public void refreshSjZfMoney(String money) {
        tv_orderpay_dealcount.setText(money);
    }

    @Override
    public void refreshPayTypeView() {
        cb_ali.setChecked(false);
        cb_weixin.setChecked(false);
        cb_qb.setChecked(false);
        cb_crash.setChecked(false);
        cb_bfb.setChecked(false);
    }


    @Override
    public void refreshBestCouponsView(ApiCouList.DataListBean info) {
        if (info != null) {
            tv_orderpay_couponscount.setText(info.getDenomination() + "");
        } else {
            tv_orderpay_couponscount.setText("0");
        }
    }

    @Override
    public void showTip(String desc) {
        Toast.makeText(BaseApplication.getInstance().getApplicationContext(), desc, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void showWaitting(String desc) {
        showWaittingDialog(desc, true);
    }

    @Override
    public void dissWaitting() {
        dismissWaittingDialog();
    }


    @Override
    public String getInputMoney() {
        return tv_orderpay_count.getText().toString().trim();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void showResultPage(String orderID, String money, boolean result) {
        startActivity(PayResultActivity.getDiyIntent(this, money, orderID, result));
        if (result)
            setResult(RESULT_OK);
        finish();
    }

    @Override
    public void finishAty() {
        finish();
    }

    @OnClick(R.id.title_left)
    public void clickBack() {
        finish();
    }


    @OnTextChanged(value = R.id.pay_orderpay_count, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void onAfterMoneyChanged(CharSequence text) {
        String inputMoney = text.toString();
        if (".".equals(inputMoney) || StringUtils.isEmpty(inputMoney))
            inputMoney = "0.0";
        presenter.refreshRealMoney(inputMoney);
    }

    @OnCheckedChanged(R.id.pay_main_qianbaozhifu_cb)
    void onCheckedQB(boolean checked) {
        if (checked)
            presenter.payTypeChange(PayPresenterImpl.PAYTYPE_QB);
    }

    @OnCheckedChanged(R.id.pay_main_alizhifu_cb)
    void onCheckedAL(boolean checked) {
        if (checked)
            presenter.payTypeChange(PayPresenterImpl.PAYTYPE_ALI);
    }

    @OnCheckedChanged(R.id.pay_main_weixinzhifu_cb)
    void onCheckedWx(boolean checked) {
        if (checked)
            presenter.payTypeChange(PayPresenterImpl.PAYTYPE_WEIXIN);
    }

    @OnCheckedChanged(R.id.pay_main_xianjinzhifu_cb)
    void onCheckedCrash(boolean checked) {
        if (checked)
            presenter.payTypeChange(PayPresenterImpl.PAYTYPE_CRASH);
    }

    @OnCheckedChanged(R.id.pay_main_bfbzf_cb)
    void onCheckedBfb(boolean checked) {
        if (checked) {
            presenter.payTypeChange(PayPresenterImpl.PAYTYPE_BFB);
        }
    }

    @OnClick(R.id.pay_main_alizhifu)
    public void clickALI() {
        refreshPayTypeView();
        cb_ali.setChecked(true);
    }

    @OnClick(R.id.pay_main_weixinzhifu)
    public void clickWX() {
        refreshPayTypeView();
        cb_weixin.setChecked(true);
    }

    @OnClick(R.id.pay_main_xianjinzhifu)
    public void clickCrash() {
        refreshPayTypeView();
        cb_crash.setChecked(true);
    }

    @OnClick(R.id.pay_main_qianbaozhifu)
    public void clickQB() {
        refreshPayTypeView();
        cb_qb.setChecked(true);
    }

    @OnClick(R.id.pay_main_bfbzf)
    public void clickBfb() {
        refreshPayTypeView();
        cb_bfb.setChecked(true);
    }

    @OnClick(R.id.pay_orderpay_coupons)
    public void clickCoupns() {
        startActivityForResult(CouponsActivity.getDiyIntent(this, presenter.getOrderType(), true, presenter.getBestCoupons()), 100);
    }

    @OnClick(R.id.pay_main_commit)
    public void clickCommit() {
        UIHelper.getAlertDialog(getActivity(), getActivity().getString(R.string.querenzhifu), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                presenter.doPay();
            }
        }, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            ApiCouList.DataListBean coupons = (ApiCouList.DataListBean) data.getSerializableExtra("data");
            presenter.refreshCoupons(coupons);
        }
    }
}
